import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:baseProjectFlutter/config/app_colors.dart';

import '../utils/store_utils.dart';

final elevatedButtonThemeMain = ElevatedButtonThemeData(
    style: ButtonStyle(
  // 设置背景颜色
  backgroundColor: WidgetStateProperty.resolveWith<Color?>(
    (Set<WidgetState> states) {
      // if (states.contains(WidgetState.pressed)) {
      //   return Colors.grey; // 按下时的颜色
      // }
      return AppColors.primaryColor; // 默认颜色
    },
  ),
  // 确保水波纹效果存在
  overlayColor: WidgetStateProperty.resolveWith<Color?>(
    (Set<WidgetState> states) {
      if (states.contains(WidgetState.pressed)) {
        return Colors.grey.withOpacity(0.5); // 按下时的水波纹颜色
      }
      return null; // 默认情况下无水波纹
    },
  ),
  // 设置文本颜色
  foregroundColor: WidgetStateProperty.all<Color>(Colors.white),
  // 设置按钮的最小尺寸
  // minimumSize: WidgetStateProperty.all<Size>(Size(88, 36)),
  // 设置边框圆角半径
  // shape: WidgetStateProperty.all<RoundedRectangleBorder>(RoundedRectangleBorder()),
));
final textButtonThemeDataMain = TextButtonThemeData(
    style: ButtonStyle(
        // 设置背景颜色
        // backgroundColor: WidgetStateProperty.all<Color>(AppColors.primaryColor),
        // 设置文本颜色
        // foregroundColor: WidgetStateProperty.all<Color>(AppColors.primaryColor),
        // 设置按钮的最小尺寸
        // minimumSize: WidgetStateProperty.all<Size>(Size(88, 36)),
        // 设置边框圆角半径
        // shape: WidgetStateProperty.all<RoundedRectangleBorder>(RoundedRectangleBorder()),
        ));

final ThemeData lightTheme = ThemeData(
  colorScheme: const ColorScheme.light(primary: AppColors.primaryColor, secondary: AppColors.colorAccent),
  useMaterial3: true,
  brightness: Brightness.light,
  primaryColor: AppColors.primaryColor,
  scaffoldBackgroundColor: Colors.transparent,
  textTheme: const TextTheme(
    bodyLarge: TextStyle(color: Colors.black),
    bodySmall: TextStyle(color: Colors.black),
    bodyMedium: TextStyle(color: Colors.black),
  ),
  textButtonTheme: textButtonThemeDataMain,
  elevatedButtonTheme: elevatedButtonThemeMain,
  //自定义颜色
  extensions: <ThemeExtension<dynamic>>[
    CustomLightColors(),
  ],
);
final ThemeData darkTheme = ThemeData(
  colorScheme: const ColorScheme.dark(primary: AppColors.primaryColorDark, secondary: AppColors.colorAccent),
  useMaterial3: true,
  brightness: Brightness.dark,
  primaryColor: AppColors.primaryColorDark,
  scaffoldBackgroundColor: Colors.transparent,
  textTheme: const TextTheme(
    bodyLarge: TextStyle(color: Colors.white),
    bodySmall: TextStyle(color: Colors.white),
    bodyMedium: TextStyle(color: Colors.white),
  ),
  textButtonTheme: textButtonThemeDataMain,
  elevatedButtonTheme: elevatedButtonThemeMain,
// 自定义颜色
  extensions: <ThemeExtension<dynamic>>[
    CustomDarkColors(),
  ],
);

class ThemeUtils {
  ThemeUtils._();

  static ThemeData getCommonAppBarTheme(BuildContext context) {
    return Theme.of(context).copyWith(
        appBarTheme: context.theme.appBarTheme.copyWith(
// color: context.theme.appBarTheme.backgroundColor,
            ));
  }
}

// 自定义 亮主题颜色
class CustomLightColors extends CustomColors {
  CustomLightColors() {
    color1 = Colors.black;
    text = Colors.black;
    homeMenuBg = Colors.white;
    homeMenuSelect = Colors.black;
  }
}

// 自定义 亮主题颜色
class CustomDarkColors extends CustomColors {
  CustomDarkColors() {
    color1 = Colors.red;
    text = Colors.white;
    homeMenuBg = Colors.white;
    homeMenuSelect = Colors.black;
  }
}

/// 自定义主题颜色
class CustomColors extends ThemeExtension<CustomColors> {
  late Color color1;
  late Color text;
  late Color homeMenuBg;
  late Color homeMenuSelect;

  CustomColors();

  @override
  CustomColors copyWith() {
    return this;
  }

  @override
  CustomColors lerp(CustomColors other, double t) {
    return this;
  }

  static CustomColors of(BuildContext context) {
    return Theme.of(context).extension<CustomColors>()!;
  }
}

extension AppThemeExtension on BuildContext {
// ThemeData get theme => Theme.of(this);

  bool get isDark => theme.brightness == Brightness.dark;

  Color get primaryColor => theme.primaryColor;

  CustomColors get appColors => CustomColors.of(this);
}

extension AppStateThemeExtension on State {
  ThemeData get theme => Theme.of(context);

  bool get isDark => theme.brightness == Brightness.dark;

  Color get primaryColor => theme.primaryColor;

  CustomColors get appColors => CustomColors.of(context);
}

/// 主题控制
class ThemeVm extends GetxController {
  final _themeMode = ThemeMode.system.obs;

// 获取当前主题
  ThemeMode get themeMode => _themeMode.value;

// 设置当前主题
  set themeMode(ThemeMode mode) {
    _themeMode.value = mode;
    _saveThemePreference(mode);
  }

// 切换主题
  void toggleTheme() {
    if (_themeMode.value == ThemeMode.light) {
      _themeMode.value = ThemeMode.dark;
    } else {
      _themeMode.value = ThemeMode.light;
    }
    _saveThemePreference(_themeMode.value);
  }

// 保存主题偏好
  Future<void> _saveThemePreference(ThemeMode mode) async {
    switch (mode) {
      case ThemeMode.system:
        StoreUtils.setInt('theme', 0);
        break;
      case ThemeMode.light:
        StoreUtils.setInt('theme', 1);
        break;
      case ThemeMode.dark:
        StoreUtils.setInt('theme', 2);
        break;
    }
  }

  @override
  void onInit() {
    super.onInit();
    _loadThemePreference();
  }

// 加载主题偏好
  Future<void> _loadThemePreference() async {
    int? themeIndex = StoreUtils.getInt('theme');
    if (themeIndex != null) {
      _themeMode.value = [ThemeMode.system, ThemeMode.light, ThemeMode.dark][themeIndex];
    }
  }
}
